#region using

using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using Controls;

#endregion

/// <summary>
/// Renders the appropriate code to display a photo via the Photo handler when a [photo] tag is found.
/// <example>
/// [photo:myphoto,200] will get a photo with the slug "myphoto" and resize it so the longest side is 200</example>
/// 
/// </summary>
[Extension("Display a single photo via the Photo handler", "0.1", "BlogEngine.NET")]
public class ShowPhoto : RegexMatchBase
{
    private static readonly Regex _BodyRegex = new Regex(@"\[photo:([A-Za-z0-9_\-]*)(,\w*)*\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);

    /// <summary>
    /// make sure the base constructs
    /// </summary>
    public ShowPhoto()
        : base()
    {
        base.Regex = _BodyRegex;
    }

    /// <summary>
    /// Handles the Page and Post Serving event to take care of the [photo] keyword.
    /// </summary>
    protected override string MatchCore(Match match)
    {
        bool inlineImage = false;
        int newSize = 0;
        string floatStyle = string.Empty;

        //lets see if there are any other parameters and they must be in order
        if (!string.IsNullOrEmpty(match.Groups[2].Value))
        {
            CaptureCollection myParams = match.Groups[2].Captures;

            for (int i = 0; i < myParams.Count; i++)
            {
                switch (i)
                {
                    //first is the maximum size
                    case 0:
                        int.TryParse(CleanTag(myParams[0].Value), out newSize);
                        break;
                    //second is whether to inline the image (anchor yes/no)
                    case 1:
                        bool.TryParse(CleanTag(myParams[1].Value), out inlineImage);
                        break;
                    //third is whether to inline the image (anchor yes/no)
                    case 2:
                        floatStyle = CleanTag(myParams[2].Value);
                        break;
                }
            }
        }

        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            DisplayPhoto photo = new DisplayPhoto();
            photo.Slug = match.Groups[1].Value;
            photo.Size = newSize;
            photo.FloatStyle = floatStyle;
            photo.Inline = inlineImage;
            photo.RenderControl(new HtmlTextWriter(sw));
            return sw.ToString();
        }

    }

    /// <summary>
    /// strip the comma from the front of the tag
    /// </summary>
    /// <param name="tag"></param>
    /// <returns></returns>
    private string CleanTag(string tag)
    {
        if (string.IsNullOrEmpty(tag))
        {
            return string.Empty;
        }
        return tag.Remove(0, 1);
    }
}
